﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PessModel;

namespace PessMvc.Controllers
{
    [Authorize]
    public class ExpertGroupController : Controller
    {
        private PESSEntities db = new PESSEntities();

        //
        // GET: /ExpertGroup/

        public ActionResult Index(int activityID)
        {
            //var proposalActivity=db.Activities.Find(activityID).ProposalActivity;
            ViewData["Subjects"] = new SelectList(db.Activities.Find(activityID).Subjects, "ID", "Name");
            ViewBag.ActivityID = activityID;
            //var expertgroups = from g in db.ExpertGroups.Include(e => e.Subject)
            //                   where g.Subject.ActivityID == activityID
            //                   select g;

            var expertgroups = db.GetExpertGroupList(activityID);
           
            return View(expertgroups.ToList());
        }

        //
        // GET: /ExpertGroup/Details/5

        public ActionResult Details(int id )
        {
            ExpertGroup expertgroup = db.ExpertGroups.Find(id);
            if (expertgroup == null)
            {
                return HttpNotFound();
            }
            return View(expertgroup);
        }

        //
        // GET: /ExpertGroup/Create

        public ActionResult Create(int activityID)
        {
            //int proposalActivityID = db.Activities.Find(activityID).ProposalActivity.ID;
            ViewBag.SubjectID = new SelectList(db.Subjects.Where(s=>s.ActivityID==activityID), "ID", "Name");
            return View();
        }

        [HttpPost]
        public ActionResult CreateConfirmed(int subjectID,string callsign)
        {
            var subject = db.Subjects.Find(subjectID);
            subject.ExpertGroups.Add(new ExpertGroup() { SubjectID = subjectID, Callsign=callsign });
            db.SaveChanges();
            return RedirectToAction("Index", "ExpertGroup", new {ActivityID=subject.ActivityID });
        }
        //
        // POST: /ExpertGroup/Create

        //[HttpPost]
        //public ActionResult Create(ExpertGroup expertgroup)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.ExpertGroups.Add(expertgroup);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    ViewBag.SubjectID = new SelectList(db.Subjects, "ID", "Name", expertgroup.SubjectID);
        //    return View(expertgroup);
        //}

        //
        // GET: /ExpertGroup/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ExpertGroup expertgroup = db.ExpertGroups.Find(id);
            if (expertgroup == null)
            {
                return HttpNotFound();
            }

            int activityID=expertgroup.Subject.ActivityID;
            ViewBag.SubjectID = new SelectList(db.Subjects.Where(s=>s.ActivityID==activityID), "ID", "Name", expertgroup.SubjectID);
            ViewBag.LeaderID = new SelectList(expertgroup.ExpertJudges, "SerialNumber", "RealName",expertgroup.LeaderID);
            return View(expertgroup);
        }

        //
        // POST: /ExpertGroup/Edit/5

        [HttpPost]
        public ActionResult Edit(ExpertGroup expertgroup)
        {
            if (ModelState.IsValid)
            {
                db.Entry(expertgroup).State = EntityState.Modified;
                db.SaveChanges();
                //return RedirectToAction("Index", new {activityID=db.Subjects.Find(expertgroup.SubjectID).ActivityID });
                return RedirectToAction("Details", new { ID = expertgroup.ID });
            }
            ViewBag.SubjectID = new SelectList(db.Subjects, "ID", "Name", expertgroup.SubjectID);
            ViewBag.LeaderID = new SelectList(expertgroup.ExpertJudges, "SerialNumber", "RealName");
            return View(expertgroup);
        }

        //
        // GET: /ExpertGroup/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ExpertGroup expertgroup = db.ExpertGroups.Find(id);
            if (expertgroup == null)
            {
                return HttpNotFound();
            }
            //if (expertgroup.ExpertJudges.Count != 0)
            //{
            //    ModelState.AddModelError("", "该评委组里存在评委，不能删除。请先删除评委！");
            //}
            ViewBag.ActivityID = expertgroup.Subject.ActivityID;
            return View(expertgroup);
        }

        //
        // POST: /ExpertGroup/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ExpertGroup expertgroup = db.ExpertGroups.Find(id);
            ViewBag.ActivityID = expertgroup.Subject.ActivityID;
            if (expertgroup.ExpertJudges.Count == 0)
            {
                db.ExpertGroups.Remove(expertgroup);
                db.SaveChanges();
                return RedirectToAction("Index", new { activityID = ViewBag.ActivityID });
            }
            else
            {
                ModelState.AddModelError("", "该评委组里存在评委，不能删除。请先删除评委！");
                return View(expertgroup);
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}